<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>倒计时器</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }
        .date-selectors {
            display: flex;
            gap: 10px;
            margin-bottom: 20px;
        }
        select, input[type="text"] {
            padding: 10px;
            border: 2px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
            flex: 1;
        }
        select:focus, input[type="text"]:focus {
            outline: none;
            border-color: #4CAF50;
        }
        input[type="text"] {
            width: 100%;
            box-sizing: border-box;
        }
        .generate-btn {
            background-color: #4CAF50;
            color: white;
            padding: 12px 30px;
            border: none;
            border-radius: 5px;
            font-size: 18px;
            cursor: pointer;
            width: 100%;
            margin-top: 20px;
        }
        .generate-btn:hover {
            background-color: #45a049;
        }
        .result {
            margin-top: 30px;
            padding: 20px;
            background-color: #f9f9f9;
            border-radius: 5px;
            border-left: 4px solid #4CAF50;
            display: none;
        }
        .result h2 {
            color: #333;
            margin-bottom: 20px;
        }
        .countdown-item {
            background: white;
            padding: 15px;
            margin: 10px 0;
            border-radius: 5px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            font-size: 18px;
        }
        .countdown-number {
            font-weight: bold;
            color: #4CAF50;
            font-size: 24px;
        }
        .error {
            color: #f44336;
            margin-top: 10px;
            display: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>倒计时器</h1>
        
        <div class="form-group">
            <label>选择目标日期：</label>
            <div class="date-selectors">
                <select id="year">
                    <option value="">年</option>
                </select>
                <select id="month">
                    <option value="">月</option>
                    <option value="1">1月</option>
                    <option value="2">2月</option>
                    <option value="3">3月</option>
                    <option value="4">4月</option>
                    <option value="5">5月</option>
                    <option value="6">6月</option>
                    <option value="7">7月</option>
                    <option value="8">8月</option>
                    <option value="9">9月</option>
                    <option value="10">10月</option>
                    <option value="11">11月</option>
                    <option value="12">12月</option>
                </select>
                <select id="day">
                    <option value="">日</option>
                </select>
            </div>
        </div>
        
        <div class="form-group">
            <label for="description">描述文字（最多20个字符）：</label>
            <input type="text" id="description" maxlength="20" placeholder="请输入描述文字...">
        </div>
        
        <button class="generate-btn" onclick="generateCountdown()">生成倒计时</button>
        
        <div class="error" id="error"></div>
        
        <div class="result" id="result">
            <h2 id="resultTitle"></h2>
            <div class="countdown-item">
                <div>距离目标日期还剩：<span class="countdown-number" id="monthsLeft"></span> 个月</div>
            </div>
            <div class="countdown-item">
                <div>距离目标日期还剩：<span class="countdown-number" id="daysLeft"></span> 天</div>
            </div>
            <div class="countdown-item">
                <div>距离目标日期还剩：<span class="countdown-number" id="secondsLeft"></span> 秒</div>
            </div>
        </div>
    </div>

    <script>
        // 初始化年份选项
        function initYears() {
            const yearSelect = document.getElementById('year');
            const currentYear = new Date().getFullYear();
            
            for (let year = currentYear; year <= currentYear + 50; year++) {
                const option = document.createElement('option');
                option.value = year;
                option.textContent = year + '年';
                yearSelect.appendChild(option);
            }
        }
        
        // 初始化日期选项
        function initDays() {
            const daySelect = document.getElementById('day');
            
            for (let day = 1; day <= 31; day++) {
                const option = document.createElement('option');
                option.value = day;
                option.textContent = day + '日';
                daySelect.appendChild(option);
            }
        }
        
        // 更新日期选项（根据月份和年份）
        function updateDays() {
            const year = parseInt(document.getElementById('year').value);
            const month = parseInt(document.getElementById('month').value);
            const daySelect = document.getElementById('day');
            
            if (!year || !month) return;
            
            // 获取该月的天数
            const daysInMonth = new Date(year, month, 0).getDate();
            
            // 清空现有选项
            daySelect.innerHTML = '<option value="">日</option>';
            
            // 添加有效的日期选项
            for (let day = 1; day <= daysInMonth; day++) {
                const option = document.createElement('option');
                option.value = day;
                option.textContent = day + '日';
                daySelect.appendChild(option);
            }
        }
        
        // 生成倒计时
        function generateCountdown() {
            const year = document.getElementById('year').value;
            const month = document.getElementById('month').value;
            const day = document.getElementById('day').value;
            const description = document.getElementById('description').value;
            const errorDiv = document.getElementById('error');
            const resultDiv = document.getElementById('result');
            
            // 隐藏之前的错误和结果
            errorDiv.style.display = 'none';
            resultDiv.style.display = 'none';
            
            // 验证输入
            if (!year || !month || !day) {
                showError('请选择完整的日期！');
                return;
            }
            
            // 创建目标日期
            const targetDate = new Date(year, month - 1, day);
            const now = new Date();
            
            // 检查日期是否有效
            if (targetDate <= now) {
                showError('目标日期必须是未来的日期！');
                return;
            }
            
            // 计算时间差
            const timeDiff = targetDate.getTime() - now.getTime();
            
            // 计算月数差
            const monthsDiff = calculateMonthsDifference(now, targetDate);
            
            // 计算天数差
            const daysDiff = Math.ceil(timeDiff / (1000 * 60 * 60 * 24));
            
            // 计算秒数差
            const secondsDiff = Math.ceil(timeDiff / 1000);
            
            // 显示结果
            document.getElementById('resultTitle').textContent = description || '倒计时结果';
            document.getElementById('monthsLeft').textContent = monthsDiff;
            document.getElementById('daysLeft').textContent = daysDiff;
            document.getElementById('secondsLeft').textContent = secondsDiff.toLocaleString();
            
            resultDiv.style.display = 'block';
            
            // 开始秒数倒计时更新
            startSecondsCountdown(targetDate);
        }
        
        // 计算月数差
        function calculateMonthsDifference(startDate, endDate) {
            const startYear = startDate.getFullYear();
            const startMonth = startDate.getMonth();
            const endYear = endDate.getFullYear();
            const endMonth = endDate.getMonth();
            
            let months = (endYear - startYear) * 12 + (endMonth - startMonth);
            
            // 如果目标日期的日数小于当前日期的日数，减去一个月
            if (endDate.getDate() < startDate.getDate()) {
                months--;
            }
            
            return Math.max(0, months);
        }
        
        // 开始秒数倒计时
        let countdownInterval;
        function startSecondsCountdown(targetDate) {
            // 清除之前的倒计时
            if (countdownInterval) {
                clearInterval(countdownInterval);
            }
            
            countdownInterval = setInterval(() => {
                const now = new Date();
                const timeDiff = targetDate.getTime() - now.getTime();
                
                if (timeDiff <= 0) {
                    document.getElementById('secondsLeft').textContent = '0';
                    clearInterval(countdownInterval);
                    return;
                }
                
                const secondsDiff = Math.ceil(timeDiff / 1000);
                document.getElementById('secondsLeft').textContent = secondsDiff.toLocaleString();
            }, 1000);
        }
        
        // 显示错误信息
        function showError(message) {
            const errorDiv = document.getElementById('error');
            errorDiv.textContent = message;
            errorDiv.style.display = 'block';
        }
        
        // 页面加载时初始化
        window.onload = function() {
            initYears();
            initDays();
            
            // 监听年份和月份变化，更新日期选项
            document.getElementById('year').addEventListener('change', updateDays);
            document.getElementById('month').addEventListener('change', updateDays);
        };
    </script>
</body>
</html>